import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<>();
        HashMap<String,Integer> map1 = new HashMap<>();
        for(String str : words){
            map1.put(str,map1.getOrDefault(str,0) + 1);
        }
        int len = words[0].length();
        for (int i = 0; i < len; i++) {
            HashMap<String,Integer> map2 = new HashMap<>();
            for (int right = i,left = i, count = 0; right + len <= s.length() ; right+=len) {
                String in = s.substring(right,right + len);
                map2.put(in, map2.getOrDefault(in,0) + 1);
                if(map2.get(in) <= map1.getOrDefault(in,0))
                    count++;

                if((right - left + 1) > len * words.length){
                    String out = s.substring(left,left + len);
                    if(map2.get(out) <= map1.getOrDefault(out,0))
                        count--;
                    map2.put(out,map2.get(out) - 1);
                    left += len;
                }

                if(count == words.length){
                    list.add(left);
                }
            }
        }
        return list;
    }
}